package com.jy.rhin.support.parsing;

import com.alibaba.fastjson.JSONArray;
import com.jy.rhin.application.enums.DocumentTypeEnum;
import com.jy.rhin.facade.dto.DocumentModule;
import com.jy.rhin.facade.dto.TemplateRespDto;
import com.jy.rhin.support.parsing.enums.RecordElementEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 会诊记录
 * @Author: lww
 * @Date: 2022/6/21 8:59
 */
@Component
public class JingShenBSFParser extends BaseDocumentParser {

    @Override
    public DocumentTypeEnum getDocumentType() {
        return DocumentTypeEnum.FOLLOW_UP_SERVICES_SEVERE_PSYCHOSIS;
    }

    /**
     * 会诊记录文档解析
     *
     * @param json
     * @param templateRespList
     */
    @Override
    public List<TemplateRespDto> documentParsing(Object json, List<TemplateRespDto> templateRespList) {
        List<TemplateRespDto> templateRespDtoList = new ArrayList<>();
        //文档头部分
        List<TemplateRespDto> headList = new ArrayList<>();
        //文档体-会诊原因部分
        List<TemplateRespDto> causeList = new ArrayList<>();
        //文档体-会诊意见部分
        List<TemplateRespDto> opinionList = new ArrayList<>();
        String arrayType = null;
        //补充对应文档部分字段List
        for (TemplateRespDto templateRespDto : templateRespList) {
            String element = templateRespDto.getYuanSuMC();
            String type = templateRespDto.getType();
            //list节点遍历完成时重新赋值
            if ((!StringUtils.isEmpty(arrayType)) && containsNumber(element) <= Integer.parseInt(arrayType.substring(arrayType.indexOf(SPOT) + 1))) {
                arrayType = null;
            }
            if (!StringUtils.isEmpty(type) || !StringUtils.isEmpty(arrayType)) {
                if (Objects.equals(type, RecordElementEnum.CAUSE.getCode()) || Objects.equals(RecordElementEnum.CAUSE.getName(), arrayType)) {
                    arrayType = RecordElementEnum.CAUSE.getName();
                    causeList.add(templateRespDto);
                } else if (Objects.equals(type, RecordElementEnum.OPINION.getCode()) || Objects.equals(RecordElementEnum.OPINION.getName(), arrayType)) {
                    arrayType = RecordElementEnum.OPINION.getName();
                    opinionList.add(templateRespDto);
                }
            } else {
                headList.add(templateRespDto);
            }
        }
        //获取value值
        pathValue(headList, json, null);
        JSONArray componentArray = objectPathArray(COMPONENT_KEY, json);
        Object causeObject = arrayPathObject(SECTION_CODE, RecordElementEnum.CAUSE.getCode(), componentArray);
        pathValue(causeList, causeObject, 3);
        Object opinionObject = arrayPathObject(SECTION_CODE, RecordElementEnum.OPINION.getCode(), componentArray);
        pathValue(opinionList, opinionObject, 3);
        templateRespDtoList.addAll(headList);
        templateRespDtoList.addAll(causeList);
        templateRespDtoList.addAll(opinionList);
        return templateRespDtoList;
    }

    @Override
    public List<DocumentModule> createBodyModule() {
        List<DocumentModule> list = new ArrayList<>();
        //文档体模块
        list.add(new DocumentModule("随访事件章节","/component/structuredBody/component/section/code[@displayName='随访事件']"));
        list.add(new DocumentModule("主要健康问题章节","/component/structuredBody/component/section/code[@code='11450-4']"));
        list.add(new DocumentModule("住院史章节","/component/structuredBody/component/section/code[@code='11336-5']"));
        list.add(new DocumentModule("实验室检查章节","/component/structuredBody/component/section/code[@code='30954-2']"));
        list.add(new DocumentModule("用药章节","/component/structuredBody/component/section/code[@code='10160-0']"));
        list.add(new DocumentModule("健康评估章节","/component/structuredBody/component/section/code[@code='51848-0']"));
        list.add(new DocumentModule("健康指导章节","/component/structuredBody/component/section/code[@code='69730-0']"));
        list.add(new DocumentModule("转诊建议章节","/component/structuredBody/component/section/code[@code='18776-1']"));
        list.add(new DocumentModule("下次随访安排章节","/component/structuredBody/component/section/code[@displayName='下次随访日期']"));
        return list;
    }

}
